<?php
require_once(dirname(dirname(__FILE__)) . '/app.php');

need_login();

$is_bucong = false;

if(!empty($login_user['bankname']) && !empty($login_user['bankaccount']))
{
	$is_bucong = true;
}
if(isset($_POST['act']) && $_POST['act'] =='uptian' )
{
	if(!empty($_POST['bankusername']) && !empty($_POST['bankname']) && !empty($_POST['bankaccount']))
	{
		Table::UpdateCache('user', $login_user['id'], array(
						'bankusername' => htmlspecialchars($_POST['bankusername']),
						'bankname' => htmlspecialchars($_POST['bankname']),
						'bankaccount' => htmlspecialchars($_POST['bankaccount']),
					));
					
		$is_bucong  = true;
		$login_user['bankusername'] = htmlspecialchars($_POST['bankusername']);
		$login_user['bankname'] = htmlspecialchars($_POST['bankname']);
		$login_user['bankaccount'] = htmlspecialchars($_POST['bankaccount']);
	}
}

if(isset($_GET['act']) && $_GET['act'] == 'shenqing')
{
	$tip ='最低提现额度'.$INI['system']['cancommission'].'元，您的余额还不足以提现。';
	if($login_user['commismoney'] >= $INI['system']['cancommission'])
	{
		$tip ='';
		
		$addr_data = array();
		$addr_data['user_id'] = $login_user_id;
		$addr_data['money'] = $INI['system']['cancommission'];
		$addr_data['state'] = 0;
		$addr_data['addtime'] = time();
		$addr_id  = DB::Insert('tixian_order', $addr_data);
		
		if($addr_id)
		{
			Table::UpdateCache('user', $login_user['id'], array(
					'commismoney' => array( "`commismoney`-{$INI['system']['cancommission']}" ),
					'dongcommismoney' => array( "`dongcommismoney`+{$INI['system']['cancommission']}" ),
				));
			$login_user['commismoney'] -= $INI['system']['cancommission'];
			
		}
	}
}

if($is_bucong)
{
	$pagetitle = '佣金提现';
	$condition = array();
	$condition['user_id'] = $login_user['id'];
	
	$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
	$per_page = 10;
	$offset = ($page - 1)*$per_page;
	
	$tixian_order = DB::LimitQuery('tixian_order', array(
		'condition' => $condition,
		'order' => 'ORDER BY addtime desc',
		'size' => $per_page,
		'offset' => $offset,
	));
	
	if($page >=2)
	{
		$s_html = '';
		$result = array('ret'=>1,'offset'=>$offset);
		if(!empty($tixian_order)){
			$result['ret'] = 0;
		}		
		
		foreach($tixian_order as $tixian)
		{
			$s_html.= '<tr>';
			$s_html.= '	<td style="text-align: center;">';
			$s_html.= $tixian['id'];							
			$s_html.= '	</td>';
			$s_html.= '	<td style="text-align: center;">';
			$s_html.= $tixian['money'];								
			$s_html.= '	</td>';
			$s_html.= '	<td style="text-align: center;">';
			if($tixian['state'] == 0)
			{
				$s_html.= '申请中';
			} else if($tixian['state'] == 1)
			{
				$s_html.= '已到账';
			} else if($tixian['state'] == 2)
			{
				$s_html.= '已拒绝';
			}
			$s_html.= '	</td>';
			$s_html.= '</tr>';
		}
		$result['html'] = $s_html;
		echo json_encode($result);
		die();
	}
	
	if(isset($INI['system']['secsite']) && !empty($INI['system']['secsite']))
		$cur_url = $INI['system']['wwwprefix'].$INI['system']['secsite'].'account/tixian.php';
	else 	
		$cur_url = $INI['system']['wwwprefix'].'account/tixian.php';

	include template('account_tixian');
} else {
	include template('account_tixian_form');
}


